<?php
/**
 * Created by PhpStorm.
 * User: wuxiang
 * Date: 2016/9/18
 * Time: 14:14
 */

namespace Api\Controller;

use Common\Libiary\LogEX;

class PushInfoController{
    private $list = [];
    public $appkey = '31e5f601b87a39a4fa3e0b6acaf789f8';

    function index(){

//        $this->list = [
//            'nonce' => $_POST['nonce'],
//            'time' => $_POST['time'],
//            'sign' => $_POST['sign'],
//        ];
//        if(!$this->checkSignTure($this->list)){
//            $result['errno'] = 1;
//            $result['msg'] = '签名错误';
//            return json_encode($result);
//        }
        LogEX::i('添加推送信息', $_POST,__FILE__,__LINE__);

        $arr['total_count'] = $_POST['TotalCount'];
        $arr['sent_count'] = $_POST['SentCount'];
        $arr['error_count'] = $_POST['ErrorCount'];
        $arr['add_time'] = time();
        //$arr['msg_id'] = $_POST['MsgID'];
        $arr['status'] = $_POST['Status'];

        LogEX::i('添加推送信息入库数据', $arr,__FILE__,__LINE__);

        $push = M('WechatPush');
        $pk = $push->where(['msg_id'=>$_POST['MsgID']])->save($arr);
        $sql = $push->getLastSql();
        LogEX::i('添加推送信息入库sql语句', ['data'=>$sql],__FILE__,__LINE__);

        $arrMery['data'] = $arr;
        $arrMery['pk'] = $pk;

        LogEX::i('添加推送信息入库', $arrMery,__FILE__,__LINE__);

        if($pk){
            $result['errno'] = 0;
            $result['msg'] = '添加成功';
            $result['data'] = $pk;
            return json_encode($result);
        }

    }

    /**
     * @param array $params
     * @return bool
     */
    protected function checkSignTure(array $params)
    {
        $signarr = $params;
        $sign = $params['sign'];
        unset($signarr['sign']);
        $str = "";
        ksort($signarr);
        foreach ($signarr as $key => $val) {
            $str .= $key . $val;
        }
        $str .= $this->appkey;
        $md5 = md5($str);
        if ($md5 == $sign) {
            return true;
        }
        return false;
    }

}
